package com.example.springmvc3;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RequestMapping("demo1")
//@RestController
@Controller
public class Colleral {
    @RequestMapping("/demo1")
    public String test(String name){
        return "my name is " + name;
    }


    @ResponseBody
    @RequestMapping("/returnJson")
    public String returnJson(@RequestBody Student student){
        return student.toString();

    }

    @RequestMapping("/m8/{id}/{name}")
    public String method8(@PathVariable Integer id , @PathVariable String name){
        return "id:" + id + "name:" + name;
    }


    //传统的获取Cookie的方法
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request,HttpServletRequest response){
        Cookie[] cookies = request.getCookies();
        System.out.println("获取Cookies的信息:");
        if(cookies!=null){
            for (Cookie cookie:cookies) {
                System.out.println(cookie.getName()+" : "+ cookie.getValue());

            }
        }
        else {
            System.out.println("这个cookie是空的");
        }
        return "获取成功";

    }
    //另一种方法
    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("Name") String dhifu){
        return "gouride:" + dhifu;


    }


    //设置Session
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        HttpSession session = request.getSession(true); //参数位true，如果session对象不存在，会自己创建一个
       session.setAttribute("name","zhangsan");
        session.setAttribute("passward","123");

        return "设置成功";

    }

    //传统的获取session的方法
    @RequestMapping("/getSession")
    public String getSession(HttpSession session){
        System.out.println("name:" + session.getAttribute("name"));
        System.out.println("Passward:" + session.getAttribute("passward"));
        return "获取完成";
    }
    //另一种获得session的方法
@RequestMapping("/getSession2")
    public String getSession2(@SessionAttribute(value = "name",required = true) String bigbig){
        return "获取密码成功"+bigbig;
    }

    //获取header
    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String  getHeader = request.getHeader("Accept");
        return "成功获取到了"+ getHeader;
    }
//    @ResponseBody
//    @RequestMapping("/setStatuse")
//    public String setStatuse(HttpServletRequest response){
//        response.setStatus(404);
//        return "状态码设置成功";
//
//    }

@RequestMapping("getIndex")
    public String getHtml(){
        return "/index.html";

    }


}
